package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.*;
import com.example.jyjava.dao.InvnDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/invn")
public class InvnContriller {
    @Autowired
    private InvnDao invnDao;

    @RequestMapping("/addInvn")
    public String addInvn(@RequestBody Invn invn){
        String falg = "400";
        invn.setCreatTime(new Date());
        invn.setUpdateTime(new Date());
        invn.setDeleteType(false);
        invn.setLook(0);
        int i = invnDao.addInvn(invn);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("invn",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getInvn")
    public String getInvn(ShopQueryInfo queryInfo){
        String falg = "400";
        int pageStart;
        int numbers;
        if (queryInfo.getPageSize() == -1){
            pageStart = 0;
            queryInfo.setPageSize(10000);
            numbers = invnDao.getInvnCounts("%" + queryInfo.getQuery() + "%",queryInfo.getUserId());
        }else{
            numbers = invnDao.getInvnCounts("%" + queryInfo.getQuery() + "%",queryInfo.getUserId());
            pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        }
        List<Invn> invns = invnDao.getInvn("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize(),queryInfo.getUserId());
//        System.out.println(invns);
        HashMap<String, Object> res = new HashMap<>();
        if (invns != null){
            falg = "200";
            res.put("flag",falg);
            res.put("invnList", invns);
            res.put("numbers",numbers);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getDelete")
    public String getDelete(@RequestBody Invn invn){
        String falg = "400";
        int i = invnDao.getDelete(invn.getId());
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("invn",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getIn")
    @ResponseBody
    public String getIn(@RequestBody Invn invn) {
        String falg = "400";
//        System.out.println(id);
        Invn i = invnDao.getIn(invn.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (i != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("invn", i);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
